package com.daiyang.lotterysystem.controller.handler;

import com.daiyang.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.daiyang.lotterysystem.common.errorexception.ControllerException;
import com.daiyang.lotterysystem.common.errorexception.ServiceException;
import com.daiyang.lotterysystem.common.pojo.CommonReslut;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler  {
    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = ServiceException.class)
    public CommonReslut<?> handleServiceException(ServiceException e) {
        //记录日志
        logger.error("serviceException:",e);

        //构造错误结果
        CommonReslut<?> reslut=new CommonReslut<>();
        reslut.setCode(GlobalErrorCodeConstants.INTERNAML_SERVICE_ERROR.getCode());
        reslut.setMsg(e.getMessage());
        return reslut;
    }
    @ExceptionHandler(value = ControllerException.class)
    public CommonReslut<?> handleControllerException(ControllerException e) {
        //记录日志
        logger.error("ControllerException:",e);

        //构造错误结果
        CommonReslut<?> reslut=new CommonReslut<>();
        reslut.setCode(GlobalErrorCodeConstants.INTERNAML_SERVICE_ERROR.getCode());
        reslut.setMsg(e.getMessage());
        return reslut;
    }
    @ExceptionHandler(value = Exception.class)
    public CommonReslut<?> handleException(Exception e) {
        //记录日志
        logger.error(" Exception:",e);

        //构造错误结果
        return CommonReslut.error(GlobalErrorCodeConstants.INTERNAML_SERVICE_ERROR.getCode(),e.getMessage());
    }
}
